package com.kaba.xtzhgl.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaba.bean.ResponseResult;
import com.kaba.bean.ResultCode;
import com.kaba.bean.SysUser;
import com.kaba.bean.SysUserPost;
import com.kaba.xtzhgl.service.SysUserPostService;
import com.kaba.xtzhgl.service.SysUserRoleService;
import com.kaba.xtzhgl.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author lvmengjie
 * @Date 2021/3/9
 * @Description TODO
 */
@RequestMapping("/sysuser")
@RestController
@CrossOrigin
public class UserContorller {


    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserPostService sysUserPostService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表显示
     * */
    @GetMapping("/queryUser")
    public ResponseResult queryUser(){
        List<SysUser> list=sysUserService.queryUser();
        return ResponseResult.success(ResultCode.QUERY_SUCCESS,list);

    }


    /**
     * 综合查询
     * */
    @PostMapping("zhcx")
    public ResponseResult zhcx(@RequestBody  SysUser sysUser){
        List<SysUser> zhcxlist=sysUserService.zhcx(sysUser);
        return ResponseResult.success(ResultCode.SUCCESS,zhcxlist);

    }



    /**
     * 添加用户信息
     * */
    @PostMapping("/saveSysUser")
    public ResponseResult saveSysUser(@RequestBody SysUser sysUser){
        sysUserService.saveSysUser(sysUser);
        List<SysUser> list=sysUserService.queryUserName(sysUser);
        System.out.println(list.get(0));
        SysUser sysUser1=new SysUser();
        sysUser1.setUserId(list.get(0).getUserId());
        sysUser1.setPostId(sysUser.getPostId());
        sysUser1.setRoleId(sysUser.getRoleId());
        sysUserPostService.savesysuserpost(sysUser1);
        sysUserRoleService.saveuserrole(sysUser1);
        return  ResponseResult.success(ResultCode.SUCCESS);

    }

    /**
     * 删除
     * */
    @DeleteMapping("deleteSysUser/{userId}")
    public ResponseResult deleteSysUser(@PathVariable Integer userId){
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("user_id",userId);
        wrapper.set("status","1");
        sysUserService.update(wrapper);
        return  ResponseResult.success(ResultCode.DELETE_SUCCESS);
    }


    /**
     * 测试接口
     * */
    @GetMapping("/querysysuserpost")
    public ResponseResult querysysuserpost(){
        List<SysUserPost> list=sysUserPostService.list();
        return  ResponseResult.success(ResultCode.SUCCESS,list);
    }

    /**
     * 根据id查询
     * */
    @GetMapping("/queryByid/{userId}")
    public ResponseResult queryByid(@PathVariable Integer userId){
        List<SysUser> idlist=sysUserService.queryByid(userId);
        return  ResponseResult.success(ResultCode.SUCCESS,idlist);
    }

    /**
     * 根据id查询
     * */
    @GetMapping("/querydeptId/{deptId}")
    public ResponseResult querydeptId(@PathVariable Integer deptId){
        List<SysUser> idlist=sysUserService.querydeptId(deptId);
        return  ResponseResult.success(ResultCode.SUCCESS,idlist);
    }


}
